package textapp.business;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import textapp.connector.push.PushMessage;
import textapp.dao.GroupDao;
import textapp.dao.GroupMessageDao;
import textapp.dao.GroupUserDao;
import textapp.dao.UserDao;
import textapp.dao.model.Group;
import textapp.dao.model.GroupMessage;
import textapp.dao.model.GroupUser;
import textapp.dao.model.User;

public class GroupMessageService {
	
	public static final boolean PUSH_GROUP_MESSAGE = true;
	
	public static void main(String[] args) {
		//new GroupMessageService().saveGroupMessage(String.valueOf(new Date().getTime()) , 1, 4, 0, 0);
//		GroupMessage gm = new GroupMessage();
//		gm.setId(8);
//		new GroupMessageService().deleteGroupMessage(7);
		
		//List<GroupMessage> msgs = new GroupMessageService().getGroupMessages(1, 1312056659896l);
		List<GroupMessage> msgs = new GroupMessageService().getGroupMessagesByUserId(1, 0);
		System.out.println(msgs.size());
	}
	
	public GroupMessage saveGroupMessage(String text, int toGroupId, int fromUserId, int type, int lifetimeMin) {
		UserDao uDao = new UserDao();
		User fromUser = uDao.findById(fromUserId);
		if (fromUser == null) {
			return null;
		}
		GroupDao gDao = new GroupDao();
		Group toGroup = gDao.getGroupById(toGroupId, false);
		if (toGroup == null) {
			return null;
		}
		GroupMessage gm = new GroupMessage();
		Date now = new Date();
		gm.setGroup(toGroup);
		gm.setFrom(fromUser);
		gm.setText(text);
		gm.setType(type);
		gm.setStatus(0);
		gm.setLifetimeMin(lifetimeMin);
		gm.setCreateDate(now);
		gm.setModifyDate(now);
		gm = new GroupMessageDao().create(gm);
		
		if (PUSH_GROUP_MESSAGE) {
			List<GroupUser> guList = new GroupUserDao().getGroupUserByGroupId(toGroup.getId());
			for (GroupUser gu : guList) {
				
				if (PushMessageService.isPushAble(gu.getUser())) {
					PushMessageService service = new PushMessageService();
					PushMessage pm = new PushMessage();			
					StringBuilder str = new StringBuilder(MessageUtil.cutStr(fromUser.getName(), 6, true));
					str.append("@");
					str.append(MessageUtil.cutStr(toGroup.getName(), 15, true));
					str.append(":");
					str.append(MessageUtil.cutStr(gm.getText(), 15, true));
					pm.setText(str.toString());
					pm.setType(PushMessage.PUSH_TYPE_GROUP_MESSAGE);
					try {
						boolean result = service.sendPushMessage(gu.getUser().getPushIdentity(), String.valueOf("U"+gu.getUser().getId()), pm, false);				
					} catch (IOException e) {
						
					}
				}
				
			}
			
		}
		
		return gm;
	}
	
	public boolean deleteGroupMessage(int gmId) {
		return new GroupMessageDao().delete(gmId);	
	}
	
	public List<GroupMessage> getGroupMessages(int groupId, long beginTimeStampMS) {
		return new GroupMessageDao().getGroupMessageByGroupAndTime(groupId, beginTimeStampMS);
	}
	
	public List<GroupMessage> getGroupMessagesByUserId(int userId, long beginTimeStampMS) {
		return new GroupMessageDao().getGroupMessageByUserIdAndTIme(userId, beginTimeStampMS);
	}
	
	
}
